package me.like.diandian.adapter;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.eicky.HeaderRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import me.like.diandian.R;
import me.like.diandian.activity.ExploreActivity;
import me.like.diandian.activity.MainActivity;
import me.like.diandian.api.Api;
import me.like.diandian.base.BaseActivity;
import me.like.diandian.base.BaseConstant;
import me.like.diandian.bean.BannerAnnounce;
import me.like.diandian.bean.BannerListBean;
import me.like.diandian.bean.LoanMarketListBean;
import me.like.diandian.databinding.HomeHeadViewBinding;
import me.like.diandian.databinding.ItemHomeLoanBinding;
import me.like.diandian.requestbean.HoweLoanMarketCountDto;
import me.like.diandian.util.CommonUtils;
import me.like.diandian.util.RxUtils;
import me.like.diandian.view.notice.BulletinView;
import me.like.diandian.view.notice.SimpleBulletinAdapter;

import static com.jakewharton.rxbinding2.view.RxView.clicks;
import static me.like.diandian.util.CommonUtils.dp2px;

/**
 * Created by shousakai on 2019/6/22.
 */

public class HomeAdapter extends HeaderRecyclerViewAdapter<HomeAdapter.HeaderViewHolder, HomeAdapter.BodyViewHolder> {

    int ll = 0;

    public void setListBeans(List<LoanMarketListBean> listBeans) {
        this.listBeans = listBeans;
        notifyDataSetChanged();
    }

    private List<LoanMarketListBean> listBeans;

    public void setBannerAnnounceList(List<BannerAnnounce> bannerAnnounceList) {
        this.bannerAnnounceList = bannerAnnounceList;
        notifyDataSetChanged();
    }

    private List<BannerAnnounce> bannerAnnounceList;
    private BaseActivity context;

    public void setBannerListBeans(List<BannerListBean> bannerListBeans) {
        this.bannerListBeans = bannerListBeans;
        notifyDataSetChanged();
    }

    private List<BannerListBean> bannerListBeans;

    public HomeAdapter(BaseActivity context, RecyclerView.LayoutManager layoutManager) {
        super(layoutManager);
        this.context = context;
        bannerListBeans = new ArrayList<>();
    }

    @Override
    protected int getHeaderCount() {
        return 1;
    }

    @Override
    protected int getItemCountForSection(int i) {

        return listBeans == null ? 0 : listBeans.size();
    }

    @Override
    protected HeaderViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup, int i) {
        View view = View.inflate(viewGroup.getContext(), R.layout.home_head_view, null);
        return new HeaderViewHolder(view);
    }

    @Override
    protected BodyViewHolder onCreateItemViewHolder(ViewGroup viewGroup, int i) {
        View view = View.inflate(viewGroup.getContext(), R.layout.item_home_loan, null);
        return new BodyViewHolder(view);
    }

    @Override
    protected void onBindHeaderViewHolder(HeaderViewHolder headerViewHolder, int i) {

        if (bannerListBeans != null && bannerListBeans.size() > 0) {
            HomePagerAdapter adapter = new HomePagerAdapter(context, bannerListBeans);
//            headerViewHolder.binding.vp.setPageMargin(dp2px(context,20));
            headerViewHolder.binding.vp.setOffscreenPageLimit(3);
            Glide.with(context).load(bannerListBeans.get(0).getPicUrl()).into(new SimpleTarget<GlideDrawable>() {
                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                    CommonUtils.setWidthAndHeight(context, headerViewHolder.binding.vp, resource.getIntrinsicWidth(), resource.getMinimumHeight());
                }
            });


            headerViewHolder.binding.vp.setAdapter(adapter);
            ll = 0;

//            RxUtils.intervel(5, TimeUnit.SECONDS, (result, data) -> {
//                ll++;
//                int size = bannerListBeans.size() > 0 ? bannerListBeans.size() : 1;
//                headerViewHolder.binding.vp.setCurrentItem(ll % size, true);
//            });
        }
        clicks(headerViewHolder.binding.llTab1).subscribe(v -> {
            ((MainActivity) context).toTab2(2);
        });
        clicks(headerViewHolder.binding.llTab2).subscribe(v -> {
            ((MainActivity) context).toTab2(0);
        });
        clicks(headerViewHolder.binding.llTab3).subscribe(v -> {
            ((MainActivity) context).toTab2(1);
        });
        if (bannerAnnounceList != null && bannerAnnounceList.size() > 0) {
            headerViewHolder.binding.llGonggao.setVisibility(View.VISIBLE);
            List<String> list = new ArrayList<>();
            for (int i1 = 0; i1 < bannerAnnounceList.size(); i1++) {
                BannerAnnounce dataBean = bannerAnnounceList.get(i1);
                list.add(dataBean.getTitle());
            }
            headerViewHolder.binding.tvHomeNotice.setAdapter(new SimpleBulletinAdapter(context, list));
            headerViewHolder.binding.tvHomeNotice.setOnBulletinItemClickListener(new BulletinView.OnBulletinItemClickListener() {
                @Override
                public void onBulletinItemClick(int position) {
//                    Toast.makeText(context, "click:" + position, Toast.LENGTH_SHORT).show();
                }
            });
        } else {
            headerViewHolder.binding.llGonggao.setVisibility(View.GONE);
        }
    }


    @Override
    protected void onBindItemViewHolder(BodyViewHolder bodyViewHolder, int i, int i1) {
        bodyViewHolder.binding.setData(listBeans.get(i1));
        String[] hotBigTitles = listBeans.get(i1).getHotBigTitle().split(",");
        TextView textView;
        bodyViewHolder.binding.llTags.removeAllViews();
        for (String s : hotBigTitles) {
            if (!TextUtils.isEmpty(s)) {
                textView = new TextView(context);
                textView.setTextColor(ActivityCompat.getColor(context, R.color.c_ff721f));
                textView.setPadding(dp2px(context, 6), dp2px(context, 2), dp2px(context, 6), dp2px(context, 2));
                textView.setBackgroundResource(R.drawable.bg_tag);
                textView.setTextSize(11);
                textView.setText(s);
                bodyViewHolder.binding.llTags.addView(textView);
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) textView.getLayoutParams();
                layoutParams.leftMargin = dp2px(context, 3);
                textView.setLayoutParams(layoutParams);
            }
        }
        Glide.with(context).load(listBeans.get(i1).getLogoUrl()).into(bodyViewHolder.binding.ivIcon);
        clicks(bodyViewHolder.binding.getRoot()).subscribe(v -> {
            Api.getInstance().getService().countLoanMarketAppRegist(new HoweLoanMarketCountDto
                    (BaseConstant.APPCHANNELID, listBeans.get(i1).getLoanId()))
                    .compose(RxUtils.applySchedulerAndAllFilter(context)).subscribe(data -> {

            });
            ExploreActivity.startActivity(context, listBeans.get(i1).getH5JumpUrl(), listBeans.get(i1).getProjectName());
        });

    }

    public class BodyViewHolder extends RecyclerView.ViewHolder {
        ItemHomeLoanBinding binding;

        public BodyViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);

        }
    }

    public class HeaderViewHolder extends RecyclerView.ViewHolder {
        HomeHeadViewBinding binding;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }
    }
}
